import { shopOrderStatus, packageStatus } from '@/hooks/useOrderStatus'
import type { ShopOrder } from '@/pages/order/types'
interface HandlerType {
    setNext: (handler: HandlerType) => HandlerType
    handle(request: ShopOrder): string
}
abstract class AbstractHandler implements HandlerType {
    private nextHandler: HandlerType | undefined
    public setNext(handler: HandlerType): HandlerType {
        this.nextHandler = handler
        return handler
    }
    public handle(request: ShopOrder): string {
        if (this.nextHandler) {
            return this.nextHandler.handle(request)
        }
        return ''
    }
}
export class ShopClose extends AbstractHandler {
    public handle(request: ShopOrder): string {
        if (request.status !== 'OK') {
            return shopOrderStatus[request.status]
        }
        return super.handle(request)
    }
}
class OrderClose extends AbstractHandler {
    public handle(request: ShopOrder): string {
        const shelvesNum = request.shopOrderItems.filter((itme) => itme.status !== 'OK').length
        if (shelvesNum === request.shopOrderItems.length) {
            return '交易失败'
        }
        return super.handle(request)
    }
}
class BeReceivedGoods extends AbstractHandler {
    public handle(request: ShopOrder): string {
        const shelvesNum = request.shopOrderItems.filter((itme) => itme.status !== 'OK').length
        const deliveryNum = request.shopOrderItems.filter((item) => item.packageStatus === 'WAITING_FOR_RECEIVE').length
        if (deliveryNum + shelvesNum === request.shopOrderItems.length) {
            return `待收货`
        }
        return super.handle(request)
    }
}
class SendGoods extends AbstractHandler {
    public handle(request: ShopOrder): string {
        //  待收货 0
        const deliveryNum = request.shopOrderItems.filter((item) => item.packageStatus === 'WAITING_FOR_RECEIVE').length
        if (deliveryNum !== 0) {
            console.log(request.shopOrderItems, 'shopOrderItems')
        }
        // 现在是分包裹 只要是一个订单的包裹，那么他们的包裹状态就是一样的取【0】即可
        return request.shopOrderItems.map((item) => packageStatus[item.packageStatus])[0]
        // return deliveryNum === 0 ? '待发货' : '部分待收货'
        // return super.handle(request)
    }
}
// 执行方法
export function clientCode(handler: HandlerType, order: ShopOrder) {
    const result = handler.handle(order)
    if (result) {
        return result
    } else {
        return '订单异常'
    }
}
const shopClose = new ShopClose()
const orderClose = new OrderClose()
const beReceivedGoods = new BeReceivedGoods()
const sendGoods = new SendGoods()
shopClose.setNext(orderClose).setNext(beReceivedGoods).setNext(sendGoods)
export const orderStatusResponsibility = (order: ShopOrder) => clientCode(shopClose, order)
